<?php

namespace app\admin\model;

use app\admin\model\pro\Birth;
use app\admin\model\pro\Breed;
use app\admin\model\pro\Daily;
use app\admin\model\pro\Disinfect;
use app\admin\model\pro\Ears;
use app\admin\model\pro\Light;
use app\admin\model\pro\Milk;
use app\admin\model\pro\Milkout;
use app\admin\model\pro\Milkpre;
use app\admin\model\pro\Out;
use app\admin\model\pro\Select;
use app\admin\model\pro\Sex;
use app\admin\model\pro\Touch;
use app\admin\model\pro\Weight;

use think\Db;
use think\Model;
use traits\model\SoftDelete;

class Batch extends Model
{

    use SoftDelete;



    // 表名
    protected $table = 'batch';

    // 自动写入时间戳字段
    //protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text'
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }


    public function getStatusList()
    {

        return ['normal' => __('Normal'), 'hidden' => __('Hidden'), 'deleted' => __('Deleted'), 'locked' => __('Locked')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public static function getPinxi($batch_id)
    {
        $item = self::alias("b")
            ->field("b.pinxi_id, p.daici, p.name")
            ->join("pinxi p", "p.id=b.pinxi_id", "left")
            ->where("b.id", $batch_id )
            ->find();

        return $item;
    }

    public function getBatchList()
    {
        $list = self::where("status", 'in', ['normal','hidden','deleted','locked'] )->field("id, name, group_id")->order("weigh DESC")->select() ;
        $list = Collection($list)->toArray();

        $list = array_column($list, 'name', 'id');
        //print_r($list) ;die;
        return $list;
    }

    // 获取某厂区:  批次-品系-库存
    public static function getFactBatchStock($factory_id, $storea_id)
    {
        $pinxiList = Pinxi::getList();
        $batch_zhongtu = Db::name("batch")->alias("b")
            ->field("sp.storea_id, b.factory_id, b.group_id, b.id as batch_id, b.name, b.pinxi_id, sum(sp.cal_num) as stock_num, sp.price")
            ->join("storepro sp", "sp.batch_id=b.id", "left")
            ->where("b.factory_id", $factory_id )
            ->where("sp.storea_id", $storea_id )
            ->group("b.id")
            ->select();
//echo Db::getLastSql(); die;
        //  3.1     todo    种兔单价获取
        foreach($batch_zhongtu as &$val){
            $val['text'] = $val['name'] . " ( {$pinxiList[$val['pinxi_id']]} ) " . " 库存: {$val['stock_num']} " ;
            $val['value'] = $val['batch_id'];
        }
        //print_r($batch_zhongtu) ;die;
        return $batch_zhongtu;
    }
    // 获取某厂区:  库位-品系-库存 (不按批次)
    public static function getFactBatchStockV2($factory_id, $storea_id)
    {
        $pinxiList = Pinxi::getList();
        $batch_zhongtu = Db::name("storepro")->alias("sp")
            ->field("sp.storea_id, sp.factory_id, sp.group_id, sp.batch_id, sp.id as sp_id, sp.field2 as pinxi_id, sum(sp.cal_num) as stock_num, sp.price")
            ->where("sp.factory_id", $factory_id )
            ->where("sp.storea_id", $storea_id )
            //->group("sp.storea_id, sp.field2")
            ->group("sp.field2")
            ->select();
//echo Db::getLastSql(); die;
        //  3.1     todo    种兔单价获取
        foreach($batch_zhongtu as &$val){
            //$val['text'] = $val['name'] . " ( {$pinxiList[$val['pinxi_id']]} ) " . " 库存: {$val['stock_num']} " ;
            $val['text'] = " ( {$pinxiList[$val['pinxi_id']]} ) " . " 库存: {$val['stock_num']} " ;
            $val['value'] = $val['pinxi_id'];
        }
        //print_r($batch_zhongtu) ;die;
        return $batch_zhongtu;
    }

    public function batchrelate(){ return $this->hasMany( Batchrelate::class ,'batch_id', 'id' )->where("deletetime", null )->where("status", "normal" )->field("*");}

    public function batchschedule(){ return $this->hasOne( Batchschedule::class ,'batch_id', 'id' )->where("deletetime", null )->field("*");}

    public function pinxi(){ return $this->hasOne( Pinxi::class ,'id', 'pinxi_id' )->field("*");}


    public function birth(){ return $this->hasMany( Birth::class ,'batch_id', 'id' )->where("status", "已通过" )->field("*");}
    public function breed(){ return $this->hasMany( Breed::class ,'batch_id', 'id' )->where("status", "已通过" )->field("*");}
    public function daily(){ return $this->hasMany( Daily::class ,'batch_id', 'id' )->where("status", "已通过" )->field("*");}
    public function disinfect(){ return $this->hasMany( Disinfect::class ,'batch_id', 'id' )->where("status", "已通过" )->field("*");}
    public function ears(){ return $this->hasMany( Ears::class ,'batch_id', 'id' )->where("status", "已通过" )->field("*");}
    public function light(){ return $this->hasMany( Light::class ,'batch_id', 'id' )->where("status", "已通过" )->field("*");}
    public function milk(){ return $this->hasMany( Milk::class ,'batch_id', 'id' )->where("status", "已通过" )->field("*");}
    public function milkout(){ return $this->hasMany( Milkout::class ,'batch_id', 'id' )->where("status", "已通过" )->field("*");}
    public function milkpre(){ return $this->hasMany( Milkpre::class ,'batch_id', 'id' )->where("status", "已通过" )->field("*");}
    public function out(){ return $this->hasMany( Out::class ,'batch_id', 'id' )->where("status", "已通过" )->field("*");}
    public function select(){ return $this->hasMany( Select::class ,'batch_id', 'id' )->where("status", "已通过" )->field("*");}
    public function sex(){ return $this->hasMany( Sex::class ,'batch_id', 'id' )->where("status", "已通过" )->field("*");}
    public function touch(){ return $this->hasMany( Touch::class ,'batch_id', 'id' )->where("status", "已通过" )->field("*");}
    public function weight(){ return $this->hasMany( Weight::class ,'batch_id', 'id' )->where("status", "已通过" )->field("*");}

}
